﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CRMAdminDashboard.CrmSvc;

namespace CRMAdminDashboard
{
    /// <summary>
    /// Wrapper for CrmSvc.privilege.
    /// </summary>
    public class SystemPrivilege
    {

        #region Properties

        /// <summary>
        /// Privilege's Guid in CRM.
        /// </summary>
        public Guid Id { get; set; }

        /// <summary>
        /// Privilege's name in CRM.
        /// </summary>
        public string PrivilegeName { get; set; }

        /// <summary>
        /// Privilege's human readable name, derived from PrivilegeName.
        /// </summary>
        public string PrivilegeNameLong { get; set; }

        /// <summary>
        /// The actual privilege in CRM
        /// </summary>
        public privilege InnerPrivilege { get; set; }

        #endregion

        #region Entry Points

        /// <summary>
        /// Standard ctor.
        /// </summary>
        public SystemPrivilege(privilege priv)
        {
            this.InnerPrivilege = priv;
            this.Id = priv.privilegeid.Value;
            this.PrivilegeName = priv.name;
            this.PrivilegeNameLong = FormatLong(priv.name);
        }

        #endregion

        #region Local Methods

        /// <summary>
        /// Create "long" name by guessing what the privilege refers to.
        /// </summary>
        private string FormatLong(string name)
        {
            if (name.Equals("prvISVExtensions"))
            {
                return "ISV Extensions";
            }
            else if (name.StartsWith("prvAppendTo"))
            {
                return InsertSpaces(name.Substring(11)) + " [APPEND TO]";
            }
            else if (name.StartsWith("prvAppend"))
            {
                return InsertSpaces(name.Substring(9)) + " [APPEND]";
            }
            else if (name.StartsWith("prvAssign"))
            {
                return InsertSpaces(name.Substring(9)) + " [ASSIGN]";
            }
            else if (name.StartsWith("prvCreate"))
            {
                return InsertSpaces(name.Substring(9)) + " [CREATE]";
            }
            else if (name.StartsWith("prvDelete"))
            {
                return InsertSpaces(name.Substring(9)) + " [DELETE]";
            }
            else if (name.StartsWith("prvShare"))
            {
                return InsertSpaces(name.Substring(8)) + " [SHARE]";
            }
            else if (name.StartsWith("prvWrite"))
            {
                return InsertSpaces(name.Substring(8)) + " [WRITE]";
            }
            else if (name.StartsWith("prvRead"))
            {
                return InsertSpaces(name.Substring(7)) + " [READ]";
            }
            else
            {
                return InsertSpaces(name.Substring(3));
            }
        }

        /// <summary>
        /// Split the privilege name on capitals. i.e.: "CreateSite" becomes "Create Site"
        /// </summary>
        private string InsertSpaces(string name)
        {
            StringBuilder sb = new StringBuilder();
            bool isFirstWord = true;

            foreach (char ch in name)
            {
                if (ch >= 'A' && ch <= 'Z')
                {
                    if (isFirstWord)
                    {
                        sb.Append(ch);
                        isFirstWord = false;
                    }
                    else
                    {
                        sb.Append(" ");
                        sb.Append(ch);
                    }
                }
                else
                {
                    sb.Append(ch);
                }
            }

            return sb.ToString();
        }

        #endregion

    }
}
